
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>与服务端交互 · React 基础学习资料</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小翼">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="02.html" />
    
    
    <link rel="prev" href="00.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    React 手稿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter01/00.html">
            
                <a href="../chapter01/00.html">
            
                    
                    基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter01/01.html">
            
                <a href="../chapter01/01.html">
            
                    
                    初识React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter01/02.html">
            
                <a href="../chapter01/02.html">
            
                    
                    React 特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter01/03.html">
            
                <a href="../chapter01/03.html">
            
                    
                    React Initial Project
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter01/04.html">
            
                <a href="../chapter01/04.html">
            
                    
                    Component和Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../chapter01/05.html">
            
                <a href="../chapter01/05.html">
            
                    
                    生态组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter02/00.html">
            
                <a href="../chapter02/00.html">
            
                    
                    组件生命周期
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter02/01.html">
            
                <a href="../chapter02/01.html">
            
                    
                    装载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter02/02.html">
            
                <a href="../chapter02/02.html">
            
                    
                    更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter02/03.html">
            
                <a href="../chapter02/03.html">
            
                    
                    卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter02/04.html">
            
                <a href="../chapter02/04.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter02/05.html">
            
                <a href="../chapter02/05.html">
            
                    
                    React.Component
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter03/00.html">
            
                <a href="../chapter03/00.html">
            
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter03/01.html">
            
                <a href="../chapter03/01.html">
            
                    
                    Component state
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter03/02.html">
            
                <a href="../chapter03/02.html">
            
                    
                    React-Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter03/03.html">
            
                <a href="../chapter03/03.html">
            
                    
                    React-Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter03/04.html">
            
                <a href="../chapter03/04.html">
            
                    
                    redux-saga
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter04/00.html">
            
                <a href="../chapter04/00.html">
            
                    
                    组件其他方法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter04/01.html">
            
                <a href="../chapter04/01.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter04/02.html">
            
                <a href="../chapter04/02.html">
            
                    
                    类属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter04/03.html">
            
                <a href="../chapter04/03.html">
            
                    
                    实例属性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter05/00.html">
            
                <a href="../chapter05/00.html">
            
                    
                    React 高级用法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter05/01.html">
            
                <a href="../chapter05/01.html">
            
                    
                    keys
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter05/02.html">
            
                <a href="../chapter05/02.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter05/03.html">
            
                <a href="../chapter05/03.html">
            
                    
                    Refs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter05/04.html">
            
                <a href="../chapter05/04.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter05/05.html">
            
                <a href="../chapter05/05.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter05/06.html">
            
                <a href="../chapter05/06.html">
            
                    
                    高阶组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter05/07.html">
            
                <a href="../chapter05/07.html">
            
                    
                    类型查检
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter05/08.html">
            
                <a href="../chapter05/08.html">
            
                    
                    代码分隔
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter06/00.html">
            
                <a href="../chapter06/00.html">
            
                    
                    React 实例
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter06/01.html">
            
                <a href="../chapter06/01.html">
            
                    
                    List
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter06/02.html">
            
                <a href="../chapter06/02.html">
            
                    
                    Form
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter06/03.html">
            
                <a href="../chapter06/03.html">
            
                    
                    Form高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter06/04.html">
            
                <a href="../chapter06/04.html">
            
                    
                    Tooltip
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="00.html">
            
                <a href="00.html">
            
                    
                    React项目打包部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.8.1" data-path="01.html">
            
                <a href="01.html">
            
                    
                    与服务端交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="02.html">
            
                <a href="02.html">
            
                    
                    打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="03.html">
            
                <a href="03.html">
            
                    
                    部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter08/00.html">
            
                <a href="../chapter08/00.html">
            
                    
                    浅谈ES
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter08/01.html">
            
                <a href="../chapter08/01.html">
            
                    
                    ES6 (ES2015)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter08/02.html">
            
                <a href="../chapter08/02.html">
            
                    
                    ES7 (ES2016)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter08/03.html">
            
                <a href="../chapter08/03.html">
            
                    
                    ES8 (ES2017)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter08/04.html">
            
                <a href="../chapter08/04.html">
            
                    
                    ES9 (ES2018)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter09/00.html">
            
                <a href="../chapter09/00.html">
            
                    
                    React Hooks
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter09/01.html">
            
                <a href="../chapter09/01.html">
            
                    
                    State Hooks useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter09/02.html">
            
                <a href="../chapter09/02.html">
            
                    
                    State Hooks useReducer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >与服务端交互</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="react&#x9879;&#x76EE;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x4EA4;&#x4E92;">React&#x9879;&#x76EE;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x4EA4;&#x4E92;</h1>
<p>web&#x9879;&#x76EE;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x6570;&#x636E;&#x901A;&#x8BAF;&#x7684;&#x65B9;&#x6CD5;&#x6709;&#x597D;&#x591A;&#x79CD;&#xFF0C;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EA;&#x4ECB;&#x7ECD;<code>Restful API</code>&#x7684;&#x65B9;&#x5F0F;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x7B80;&#x5355;&#x8BF4;&#x4E00;&#x4E0B;&#x63A5;&#x53E3;&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5728;&#x5B9E;&#x9645;&#x9879;&#x76EE;&#x4E2D;&#x53D1;&#x73B0;&#x6709;&#x5F88;&#x591A;&#x90FD;&#x4E0D;&#x662F;&#x5F88;&#x89C4;&#x8303;&#x3002;&#x5EFA;&#x8BAE;&#x5927;&#x5BB6;&#x53C2;&#x8003;&#x5B98;&#x65B9;&#x8D44;&#x6599;&#x8BE6;&#x7EC6;&#x4E86;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x4E3E;&#x4F8B;&#x8BF4;&#x660E;&#xFF1A;</p>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x63A5;&#x53E3;&#xFF1A; <code>/api/user</code> &#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x5E94;&#x8BE5;&#x5177;&#x5907;&#x56DB;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li><p><code>POST</code> &#x4FDD;&#x5B58;&#x7528;&#x6237;&#x5B9E;&#x4F53;</p>
</li>
<li><p><code>GET</code> &#x83B7;&#x53D6;&#x7528;&#x6237;&#x5217;&#x8868;&#xFF0C;&#x83B7;&#x53D6;&#x5355;&#x4E2A;&#x7528;&#x6237;&#xFF1A;<code>/api/user/{userId}</code></p>
</li>
<li><p><code>PUT</code> &#x4FEE;&#x6539;&#x7528;&#x6237;&#xFF0C;&#x4E0E;POST&#x533A;&#x522B;&#x7684;&#x662F;&#x8FD9;&#x91CC;&#x5728; <code>body</code> &#x4E2D;&#x4F20;&#x7684;&#x7528;&#x6237;&#x5B9E;&#x4F53;&#x5305;&#x542B; <code>userId</code>.</p>
</li>
<li><p><code>DELETE</code> &#x5220;&#x9664;&#x7528;&#x6237;&#x3002;&#x5C3D;&#x91CF;&#x4E0D;&#x8981;&#x4F7F;&#x7528; <code>body</code> &#x4F20; <code>userId</code>&#xFF0C;&#x90E8;&#x5206;&#x6D4F;&#x89C8;&#x5668;&#x4E0D;&#x652F;&#x6301;&#x3002;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528; <code>/api/user/{userId}</code></p>
</li>
</ul>
<p>&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x90FD;&#x4E3A; <code>JSON</code> &#x683C;&#x5F0F;&#x3002;</p>
<blockquote>
<p>&#x6211;&#x4EEC;&#x4E00;&#x5B9A;&#x8981;&#x5C06;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x7684;&#x6240;&#x6709;&#x63A5;&#x53E3;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7EDF;&#x4E00;&#x7684; URl&#xFF0C;&#x4EE5;&#x4FBF;&#x6211;&#x4EEC;&#x53D1;&#x90E8;&#x540E;&#x7684;&#x914D;&#x7F6E;&#x3002;</p>
</blockquote>
<p>&#x6211;&#x4EEC;&#x5728;&#x4ECB;&#x7ECD; <a href="../chapter03/04.html">redux-saga</a> &#x7684;&#x65F6;&#x5019;&#x63D0;&#x5230;&#x4E86;&#x4E00;&#x4E2A; <code>fetchUserApi</code> ,&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x63A5;&#x53E3;&#x83B7;&#x53D6;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x4ECB;&#x7ECD;&#x4E24;&#x4E2A;&#x5E38;&#x7528;&#x7684;ajax&#x5F00;&#x6E90;&#x5E93;&#x3002;</p>
<ul>
<li><p>axios</p>
<p><a href="https://github.com/axios/axios" target="_blank">axios</a> &#x57FA;&#x4E8E;Promise&#x7684;HTTP&#x8BF7;&#x6C42;&#x5BA2;&#x6237;&#x7AEF;&#x5E93;&#xFF0C;&#x5728;node&#x548C;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x90FD;&#x53EF;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x5B98;&#x65B9;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> axios = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;axios&apos;</span>);
<span class="hljs-comment">// Make a request for a user with a given ID</span>
axios.get(<span class="hljs-string">&apos;/user?ID=12345&apos;</span>)
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-comment">// handle success</span>
    <span class="hljs-built_in">console</span>.log(response);
  })
  .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-comment">// handle error</span>
    <span class="hljs-built_in">console</span>.log(error);
  })
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// always executed</span>
  });
</code></pre>
</li>
<li><p>isomorphic-fetch</p>
<p><a href="https://github.com/matthew-andrews/isomorphic-fetch" target="_blank">isomorphic-fetch</a>&#xFF0C;&#x8FD9;&#x662F;&#x4E00;&#x57FA;&#x4E8E; <code>window.fetch</code> &#x7684;&#x57AB;&#x7247;(polyfill)&#xFF0C;&#x63A8;&#x8350;&#x5927;&#x5BB6;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x7B80;&#x5355;&#x5C01;&#x88C5;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;es6-promise&apos;</span>;
<span class="hljs-keyword">import</span> fetch <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;isomorphic-fetch&apos;</span>;

<span class="hljs-keyword">const</span> checkStatus = (response) =&gt; {
  <span class="hljs-keyword">if</span> (response.status &gt;= <span class="hljs-number">200</span> &amp;&amp; response.status &lt; <span class="hljs-number">300</span>) {
    <span class="hljs-keyword">return</span> response;
  }
  <span class="hljs-keyword">const</span> error = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(response.statusText);
  error.response = response;
  <span class="hljs-keyword">throw</span> error;
}

<span class="hljs-comment">/**
 * Requests a URL, returning a promise.
*
* @param  {string} url       The URL we want to request
* @param  {object} [options] The options we want to pass to &quot;fetch&quot;
* @return {object}           An object containing either &quot;data&quot; or &quot;err&quot;
*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> request = (url, options) =&gt; {
  <span class="hljs-keyword">const</span> defaultOptions = {
    credentials: <span class="hljs-string">&apos;include&apos;</span> <span class="hljs-comment">//&#x5982;&#x679C;&#x4E0D;&#x5E26;cookie, &#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x8BBE;&#x7F6E;</span>
  };
  <span class="hljs-keyword">const</span> newOptions = { ...defaultOptions, ...options };
  <span class="hljs-keyword">if</span> (newOptions.method === <span class="hljs-string">&apos;POST&apos;</span> || newOptions.method === <span class="hljs-string">&apos;PUT&apos;</span>) {
    newOptions.headers = {
      Accept: <span class="hljs-string">&apos;application/json&apos;</span>,
      <span class="hljs-string">&apos;Content-Type&apos;</span>: <span class="hljs-string">&apos;application/json; charset=utf-8&apos;</span>,
      ...newOptions.headers,
    };
    newOptions.body = <span class="hljs-built_in">JSON</span>.stringify(newOptions.body);
  }

  <span class="hljs-keyword">return</span> fetch(url, newOptions)
    .then(checkStatus)
    .then(response =&gt; response.json())
    .catch(error =&gt; error);
}
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x5B89;&#x88C5; <code>isomorphic-fetch</code> &#x65F6;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x5B89;&#x88C5;<code>es6-promise</code>&#xFF0C;&#x517C;&#x5BB9;&#x5176;&#x4ED6;&#x4E00;&#x4E9B;&#x4F4E;&#x7248;&#x672C;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x3002;</p>
</blockquote>
</li>
</ul>
<p>&#x6709;&#x4E86;&#x4E0A;&#x9762;&#x7684;&#x5C01;&#x88C5;&#xFF0C;&#x6211;&#x4EEC;&#x6240;&#x6709;&#x63A5;&#x53E3;&#x8C03;&#x7528;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;<code>fetchUserApi</code></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> request <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./util/fetch.js&apos;</span>; <span class="hljs-comment">//&#x4E0A;&#x8FF0;&#x5C01;&#x88C5;&#x7684;&#x6587;&#x4EF6;&#x3002;</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> fetchUserApi = ({userId}) {
  <span class="hljs-keyword">return</span> request(<span class="hljs-string">`/api/user/<span class="hljs-subst">${userId}</span>`</span>, {method: <span class="hljs-string">&apos;GET&apos;</span>});
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x5927;&#x591A;&#x6570;&#x636E;&#x9879;&#x76EE;&#x90FD;&#x662F;&#x524D;&#x540E;&#x7AEF;&#x5206;&#x79BB;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x63A5;&#x53E3;&#x4E0D;&#x5728;web&#x6240;&#x8FD0;&#x884C;&#x7684;&#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x60F3;&#x8981;&#x5728; <code>localhost</code> &#x73AF;&#x5883;&#x4E0B;&#x8BBF;&#x95EE;&#x5176;&#x4ED6; <code>ip</code> &#x7684;&#x670D;&#x52A1;&#xFF0C;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x914D;&#x7F6E; <code>webpack</code> &#x7684; <code>proxy</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x7684;&#x9879;&#x76EE;&#x662F;<code>create-react-app</code>&#x521B;&#x5EFA;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x9700;&#x8981;&#x5728;package.json&#x4E2D;&#x914D;&#x7F6E; <code>proxy</code>&#xFF0C;&#x5982;&#x679C;&#x6709;  <code>.webpack.config.js</code>&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x662F;&#x914D;&#x7F6E; <code>proxy</code>&#x3002;</p>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-json"><span class="hljs-string">&quot;proxy&quot;</span>: {
    <span class="hljs-string">&quot;/api&quot;</span>: {
       <span class="hljs-string">&quot;target&quot;</span>: <span class="hljs-string">&quot;http://192.168.0.10:8080/&quot;</span>
     }
  }
</code></pre>
<p>&#x5230;&#x6B64;&#xFF0C;&#x4F60;&#x7684;&#x9879;&#x76EE;&#x5C31;&#x53EF;&#x4EE5;&#x5B8C;&#x6574;&#x8C03;&#x7528;&#x670D;&#x52A1;&#x7AEF;&#x63A5;&#x53E3;&#x4E86;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="00.html" class="navigation navigation-prev " aria-label="Previous page: React项目打包部署">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="02.html" class="navigation navigation-next " aria-label="Next page: 打包">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"与服务端交互","level":"1.8.1","depth":2,"next":{"title":"打包","level":"1.8.2","depth":2,"path":"chapter07/02.md","ref":"./chapter07/02.md","articles":[]},"previous":{"title":"React项目打包部署","level":"1.8","depth":1,"path":"chapter07/00.md","ref":"./chapter07/00.md","articles":[{"title":"与服务端交互","level":"1.8.1","depth":2,"path":"chapter07/01.md","ref":"./chapter07/01.md","articles":[]},{"title":"打包","level":"1.8.2","depth":2,"path":"chapter07/02.md","ref":"./chapter07/02.md","articles":[]},{"title":"部署","level":"1.8.3","depth":2,"path":"chapter07/03.md","ref":"./chapter07/03.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":[],"root":"./books","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小翼","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React 基础学习资料","language":"zh-hans","links":{"gitbook":false,"sharing":{"google":true,"facebook":true,"twitter":true}},"gitbook":"*"},"file":{"path":"chapter07/01.md","mtime":"2020-03-07T07:33:43.870Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-09T08:08:41.080Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

